package com.ruibang.glass.produce.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.produce.entity.MaterialWarehouse;
import com.ruibang.glass.produce.service.MaterialWarehouseService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 料仓管理 前端控制器
 * </p>
 *
 * @author wujie
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/api/produce/material/warehouse")
@Api(value="料仓管理控制器",tags={"料仓管理接口"})
public class MaterialWarehouseController {

    
    @Autowired
    private MaterialWarehouseService materialWarehouseService;

    
    /**
    * 通过id查询
    */
    @GetMapping("/getById/{id}")
    @ApiOperation(value="获取料仓管理",notes="获取料仓管理")
    public ResultBody getById(@PathVariable(value = "id") Integer id){
        return ResultBody.success(materialWarehouseService.getById(id));
    }

    /**
    * 新增
    */
    @PostMapping("/save")
    @ApiOperation(value="新增料仓管理",notes="新增料仓管理")
    public ResultBody save(@RequestBody MaterialWarehouse materialWarehouse){
        materialWarehouseService.save(materialWarehouse);
        return ResultBody.success();
    }

    /**
    * 通过id删除
    */
    @GetMapping("/deleteById/{id}")
    @ApiOperation(value="通过ID删除料仓管理",notes="通过ID删除料仓管理")
    public ResultBody delete(@PathVariable(value = "id") String id){
        materialWarehouseService.removeById(id);
        return ResultBody.success();
    }

    /**
    * 修改
    */
    @PostMapping("/update")
    @ApiOperation(value="修改料仓管理",notes="修改料仓管理")
    public ResultBody updateById(@RequestBody MaterialWarehouse materialWarehouse){
        materialWarehouseService.updateById(materialWarehouse);
        return ResultBody.success();
    }


    /**
    * 查询列表
    */
    @PostMapping("/list")
    @ApiOperation(value="非分页列表查询料仓管理",notes="非分页列表查询料仓管理")
    public ResultBody list(@RequestBody MaterialWarehouseReqVo materialWarehouse ){
    final LambdaQueryWrapper<MaterialWarehouse> lambda = new QueryWrapper<MaterialWarehouse>().lambda();
        this.buildCondition(lambda,materialWarehouse);
        return ResultBody.success(materialWarehouseService.list(lambda));
    }

    /**
    * 分页查询
    */
    @PostMapping("/page")
    @ApiOperation(value="分页列表查询料仓管理",notes="分页列表查询料仓管理")
    public ResultBody page(@RequestBody PageRequest<MaterialWarehouseReqVo> pageParam){
        final MaterialWarehouseReqVo param = pageParam.getParam();
        final LambdaQueryWrapper<MaterialWarehouse> lambda = new QueryWrapper<MaterialWarehouse>().lambda();
        this.buildCondition(lambda,param);
        final IPage<MaterialWarehouse> page = materialWarehouseService.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda);
        PageResult<MaterialWarehouse> pr = new PageResult(page);
        return ResultBody.success(pr);
    }


        /**
        * 构造查询条件
        * @param lambda
        * @param param
        */
        private void buildCondition(LambdaQueryWrapper<MaterialWarehouse> lambda, MaterialWarehouseReqVo param){
            if(null == param) return;
            if(!StringUtils.isEmpty(param.getMaterialWarehouseCode())){
                lambda.like(MaterialWarehouse::getMaterialWarehouseCode, param.getMaterialWarehouseCode());
            }
            if(!StringUtils.isEmpty(param.getMaterialWarehouseName())){
                lambda.like(MaterialWarehouse::getMaterialWarehouseName, param.getMaterialWarehouseName());
            }
            if(!CollectionUtils.isEmpty(param.getCreateTimeList())){
                lambda.ge(MaterialWarehouse::getCreateTime, param.getCreateTimeList().get(0));
                lambda.le(MaterialWarehouse::getCreateTime, param.getCreateTimeList().get(1));
            }
            lambda.orderBy(true,false, MaterialWarehouse::getCreateTime);
        }


        /**
         * 请求model
         */
        @Data
        private static class MaterialWarehouseReqVo extends MaterialWarehouse {
            private List<String> createTimeList; // 创建时间起止
            private List<String> updateTimeList; // 更新时间起止
            private String materialWarehouseName;
        }


}
